<template>
  <div class="app-container">
    <div class="filter-container" style="text-align: center;">
      <el-input  v-model="searchname" placeholder="根据文件名搜索" style="width: 400px;margin-right: 10px;margin-left: 30px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">
        搜索
      </el-button>

    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"

    >
     <el-table-column align="center" label="序号" width="95">
              <template slot-scope="row">
                {{ row.$index+1 }}
              </template>
            </el-table-column>
      <el-table-column label="文件名" width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.fileName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件类型" width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.ext }}</span>
        </template>
      </el-table-column>
      <el-table-column label="密码" width="200px" align="center">
        <template slot-scope="{row}">
          <span v-if="row.pwd!=null">{{ row.pwd }}</span>
          <span v-if="row.pwd==null">无</span>
          <span v-if="row.pwd==''">无</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.updateTime }}</span>
        </template>
      </el-table-column>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="warning" icon="el-icon-edit-outline" size="mini" @click="findOne(row)">
            修改
          </el-button>
          <el-button v-if="checkPermission([1])"  icon="el-icon-delete"  size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog  :visible.sync="dialogFormVisible" >
      <el-form  :model="file" label-width="70px" style="width: 400px;margin-left: 20%;" >
        <el-form-item label="文件名" prop="fileName">
          <el-input v-model="file.fileName" />
        </el-form-item>
        <el-form-item  label="文件类型" prop="ext">
          <el-input v-model="file.ext" />
        </el-form-item>
        <el-form-item  label="文件密码" prop="pwd">
          <el-input v-model="file.pwd"  />
        </el-form-item>
    </el-form>
       <div slot="footer" class="dialog-footer">
         <el-button @click="dialogFormVisible = false">
           取消
         </el-button>
         <el-button type="primary" @click="update()">
           提交
         </el-button>
       </div>
    </el-dialog>
    <el-dialog  :visible.sync="dialogFormVisible2" >
      <el-form  :model="file" label-width="70px" style="width: 400px;margin-left: 20%;" >
        <el-form-item label="文件名" prop="fileName">
          <el-input v-model="file.fileName" />
        </el-form-item>
        <el-form-item  label="文件类型" prop="ext">
          <el-input v-model="file.ext" />
        </el-form-item>
    </el-form>
       <div slot="footer" class="dialog-footer">
         <el-button @click="dialogFormVisible = false">
           取消
         </el-button>
         <el-button type="primary" @click="update()">
           提交
         </el-button>
       </div>
    </el-dialog>

  </div>
</template>

<script>
import {findFiles,getUpdate,delFile,searchFiles} from '@/api/file'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import checkPermission from '@/utils/permission' // 权限判断函数

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {

      file:{
        fileName:null,
        ext:null,
        pwd:null,
        fileId:null
      },
      value: null,
      searchname:null,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      dialogFormVisible: false,
      dialogFormVisible2:false,
      dialogStatus: '',
      dialogPvVisible: false,
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
     checkPermission,
   findOne(row){
      this.file=row
      if(row.pwd==null||row.pwd==''){
         this. dialogFormVisible=false
         this. dialogFormVisible2=true
      }else{
        this. dialogFormVisible=true
        this. dialogFormVisible2=false
      }

    },
    update(){
      getUpdate(this.file).then(res=>{
        this. dialogFormVisible=false
        this.$notify({
          message: res.status,
          type: 'success',
          duration: 2000
        })
      })

    },
    getList() {
      this.listLoading = true
      findFiles(this.listQuery).then(response => {
          this.list = response.data.items
          this.total = response.data.total
          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)
        })
      },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDelete(row) {
		var query={
			fileId:row.fileId
		}
		delFile(query).then(res=>{
			this.$notify({
			title: '删除',
			message: res.status,
			type: 'success',
			duration: 2000
		})
       this.getList()
      })

    },
    handleSearch(){
      if(this.searchname==null||this.searchname==''){
         this.getList();
      }else{
     searchFiles(this.searchname).then(res=>{
          this.total=res.data.total,
          this.list=res.data.items
        })
      }

    }
  }
}
</script>
